
package boggle;

import java.util.Random;


public class BoggleBoard {
	private String[][] grid;
	private int size;
	public static final int DEFAULT_BOARD_SIZE = 4;
	
	public BoggleBoard(int size) {
		this.size = size;
		grid = new String[size][size];
		generateBoard(grid);
	}
	
	public BoggleBoard()  { this(DEFAULT_BOARD_SIZE);	}
	
	/**
	 * Populate the grid with randomly generated strings(one character)
	 * the sampleString is according to the frequency of english characters
	 * used in real life.
	 * @param grid , the grid to populate.
	 */
	private void generateBoard(String[][] grid) {
		//String  sampleString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String  sampleString = "AAAAAAAABBBCCCDDDDEEEEEEEEEEFFGGG" +
				"HHHIIIIIIIJKKLLLLLLMMMNNNNN" +
				"OOOOOOPPPQRRRRSSSSSTTTTTUUUUVVWWXYYYZ";
		Random r = new Random();
		int numRows = grid.length;
		int numCols = grid[0].length;
		for(int i = 0; i < numRows; i++){
			for (int j = 0; j < numCols; j++) {
				int index = r.nextInt(sampleString.length());
				char  ch = sampleString.charAt(index);
				if (ch == 'Q') grid[i][j] = "Qu";
				else    	   grid[i][j] = ch + "";
			}
		}
	}

	/**
	 * generate a board for game start.
	 * the grid contains "BOGGLE" in specified position
	 */
	public void setToGameStartBoard() {
		int numRows = grid.length;
		int numCols = grid[0].length;
		for(int i = 0; i < numRows; i++){
			for (int j = 0; j < numCols; j++) {
				 grid[i][j] = " ";
			}
		}
		grid[0][0] = "B";
		grid[0][1] = "O";
		grid[1][1] = "G";
		grid[2][1] = "G";
		grid[3][1] = "L";
		grid[3][2] = "E";
	}
	
	/**
	 * return the size of BoggleBoard
	 */
	public int size() {	return grid.length;	}
	
	/**
	 * get the string at index (row, col)
	 * @param row , row index
	 * @param col , column index
	 * @return the string
	 */
	public String getCell(int row, int col) { return grid[row][col]; }
	
	/**
	 * determine whether or not (row, col) is in board bound
	 * @param row , row index
	 * @param col , column index
	 * @return , true if in bound, false otherwise.
	 */
	public boolean  inBoardBound(int row, int col) {
		return row >= 0 && row < size && col >= 0 && col < size;
	}
	
	@Override
	public String toString() {
		String res = "";
		for (int i = 0; i < size(); i++) {
			for (int j = 0; j < size(); j++) {
				res += getCell(i, j);
			}
			res += "\n";
		}
		return res;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			BoggleBoard bd = new BoggleBoard();
			System.out.println(bd);
			System.out.println("==============");
		}
	}

}
